package com.yiya.ms.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.yiya.bean.Express;
import com.yiya.bean.SysUser;
import com.yiya.model.ExpressCplex;
import com.yiya.model.ExpressCplexModel;
import com.yiya.service.ExpressService;
import com.yiya.service.SysRoleService;
import com.yiya.utils.HtmlUtil;
import com.yiya.utils.SessionUtils;

@Controller
@RequestMapping("/express")
public class ExpressAction extends BaseAction {
	@SuppressWarnings("unused")
	private final static Logger log = Logger.getLogger(ExpressAction.class);

	@Autowired
	private ExpressService expressService;
	
	@SuppressWarnings("rawtypes")
	@Autowired
	private SysRoleService sysRoleService;
	
	@InitBinder
	protected void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(
				dateFormat, false));
	}
	
	@RequestMapping("/list")
	public ModelAndView list(HttpServletRequest request) {
		Map<String, Object> context = getRootMap();
		return forword("express/express", context);
	}
	
	@RequestMapping("/dataList")
	public void datalist(ExpressCplexModel model,HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		SysUser user = SessionUtils.getUser(request);
		int scope = sysRoleService.queryUserViewScope(user.getId());
		if(scope!=1){
			model.setCreateBy(user);
		}
		List<ExpressCplex> list = expressService.queryByList(model);
		int count = expressService.queryByCount(model);
		// 设置页面数据
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		jsonMap.put("rows", list);
		jsonMap.put("total", count);
		HtmlUtil.writerJson(response, jsonMap);
	}
	
	@RequestMapping("/save")
	public void save(Express bean,HttpServletRequest request,HttpServletResponse response){
		Date nowTime = new Date();
		SysUser user = SessionUtils.getUser(request);
		if(bean.getId() == null){
			bean.setCreateTime(nowTime);
			bean.setCreateBy(user.getId());
			expressService.add(bean);
		}else{
			bean.setUpdateBy(user.getId());
			bean.setUpdateTime(nowTime);
			expressService.Update(bean);
		}
		sendSuccessMessage(response, "保存成功");
	}
	
	@RequestMapping("/getId")
	public void getId(Integer id,HttpServletResponse response){
		Map<String,Object>  context = getRootMap();
		Express express = expressService.queryById(id);
		if(express  == null){
			sendFailureMessage(response, "没有找到对应的记录!");
			return;
		}
		context.put(SUCCESS, true);
		context.put("data", express);
		HtmlUtil.writerJson(response, context);
	}
	
	@RequestMapping("/delete")
	public void delete(Integer[] id,HttpServletRequest request,HttpServletResponse response) throws Exception{
		SysUser user = SessionUtils.getUser(request);
		expressService.delete(id,user);
		sendSuccessMessage(response, "删除成功");
	}

}
